home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tcl / dist6.3 / tests / for.test < prev    next >
Encoding:
Text File  |  1991-07-24  |  4.2 KB  |  159 lines

  1. # Commands covered:  foreach, for, continue, break
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /sprite/src/lib/tcl/tests/RCS/for.test,v 1.7 91/07/23 21:01:05 ouster Exp $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. # Basic "foreach" operation.
  21.  
  22. test for-1.1 {basic foreach tests} {
  23.     set a {}
  24.     foreach i {a b c d} {
  25.     set a [concat $a $i]
  26.     }
  27.     set a
  28. } {a b c d}
  29. test for-1.2 {basic foreach tests} {
  30.     set a {}
  31.     foreach i {a b {{c d} e} {123 {{x}}}} {
  32.     set a [concat $a $i]
  33.     }
  34.     set a
  35. } {a b {c d} e 123 {{x}}}
  36. test for-1.3 {basic foreach tests} {catch {foreach} msg} 1
  37. test for-1.4 {basic foreach tests} {
  38.     catch {foreach} msg
  39.     set msg
  40. } {wrong # args: should be "foreach varName list command"}
  41. test for-1.5 {basic foreach tests} {catch {foreach i} msg} 1
  42. test for-1.6 {basic foreach tests} {
  43.     catch {foreach i} msg
  44.     set msg
  45. } {wrong # args: should be "foreach varName list command"}
  46. test for-1.7 {basic foreach tests} {catch {foreach i j} msg} 1
  47. test for-1.8 {basic foreach tests} {
  48.     catch {foreach i j} msg
  49.     set msg
  50. } {wrong # args: should be "foreach varName list command"}
  51. test for-1.9 {basic foreach tests} {catch {foreach i j k l} msg} 1
  52. test for-1.10 {basic foreach tests} {
  53.     catch {foreach i j k l} msg
  54.     set msg
  55. } {wrong # args: should be "foreach varName list command"}
  56. test for-1.11 {basic foreach tests} {
  57.     set a {}
  58.     foreach i {} {
  59.     set a [concat $a $i]
  60.     }
  61.     set a
  62. } {}
  63. test for-1.11 {foreach errors} {
  64.     catch {unset a}
  65.     set a(0) 44
  66.     list [catch {foreach a {1 2 3} {}} msg] $msg
  67. } {1 {couldn't set loop variable}}
  68. catch {unset a}
  69.  
  70. # Check "continue".
  71.  
  72. test for-2.1 {continue tests} {catch continue} 4
  73. test for-2.2 {continue tests} {
  74.     set a {}
  75.     foreach i {a b c d} {
  76.     if {[string compare $i "b"] == 0} continue
  77.     set a [concat $a $i]
  78.     }
  79.     set a
  80. } {a c d}
  81. test for-2.3 {continue tests} {
  82.     set a {}
  83.     foreach i {a b c d} {
  84.     if {[string compare $i "b"] != 0} continue
  85.     set a [concat $a $i]
  86.     }
  87.     set a
  88. } {b}
  89. test for-2.4 {continue tests} {catch {continue foo} msg} 1
  90. test for-2.5 {continue tests} {
  91.     catch {continue foo} msg
  92.     set msg
  93. } {wrong # args: should be "continue"}
  94.  
  95. # Check "break".
  96.  
  97. test for-3.1 {break tests} {catch break} 3
  98. test for-3.2 {break tests} {
  99.     set a {}
  100.     foreach i {a b c d} {
  101.     if {[string compare $i "c"] == 0} break
  102.     set a [concat $a $i]
  103.     }
  104.     set a
  105. } {a b}
  106. test for-3.3 {break tests} {catch {break foo} msg} 1
  107. test for-3.4 {break tests} {
  108.     catch {break foo} msg
  109.     set msg
  110. } {wrong # args: should be "break"}
  111.  
  112. # Check "for" and its use of continue and break.
  113.  
  114. test for-4.1 {for tests} {
  115.     set a {}
  116.     for {set i 1} {$i<6} {set i [expr $i+1]} {
  117.     set a [concat $a $i]
  118.     }
  119.     set a
  120. } {1 2 3 4 5}
  121. test for-4.2 {for tests} {
  122.     set a {}
  123.     for {set i 1} {$i<6} {set i [expr $i+1]} {
  124.     if $i==4 continue
  125.     set a [concat $a $i]
  126.     }
  127.     set a
  128. } {1 2 3 5}
  129. test for-4.3 {for tests} {
  130.     set a {}
  131.     for {set i 1} {$i<6} {set i [expr $i+1]} {
  132.     if $i==4 break
  133.     set a [concat $a $i]
  134.     }
  135.     set a
  136. } {1 2 3}
  137. test for-4.4 {for tests} {catch {for 1 2 3} msg} 1
  138. test for-4.5 {for tests} {
  139.     catch {for 1 2 3} msg
  140.     set msg
  141. } {wrong # args: should be "for start test next command"}
  142. test for-4.6 {for tests} {catch {for 1 2 3 4 5} msg} 1
  143. test for-4.7 {for tests} {
  144.     catch {for 1 2 3 4 5} msg
  145.     set msg
  146. } {wrong # args: should be "for start test next command"}
  147. test for-4.8 {for tests} {
  148.     set a {xyz}
  149.     for {set i 1} {$i<6} {set i [expr $i+1]} {}
  150.     set a
  151. } xyz
  152. test for-4.9 {for tests} {
  153.     set a {}
  154.     for {set i 1} {$i<6} {set i [expr $i+1]; if $i==4 break} {
  155.     set a [concat $a $i]
  156.     }
  157.     set a
  158. } {1 2 3}
  159.